package com.zhugang.week13.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @program algorithms
 * @description: levelOrder3
 * @author: chanzhugang
 * @create: 2022/12/28 15:35
 */
public class LevelOrder3 {

    /**
     * 102 二叉树的层序遍历（分组展示）
     * https://leetcode.cn/problems/binary-tree-level-order-traversal/
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        /**
         * 非递归实现：队列实现层序遍历
         * 每一层一个分组，需要记录节点属于哪一层
         */
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Queue<TreeNodeWithLevel> queue = new LinkedList<>();
        queue.add(new TreeNodeWithLevel(root, 0));
        while (!queue.isEmpty()) {
            TreeNodeWithLevel tl = queue.poll();
            if (tl.level > result.size() - 1) {
                result.add(new ArrayList<>());
            }
            result.get(tl.level).add(tl.treeNode.val);
            if (tl.treeNode.left != null) {
                // 记录节点属于哪一层
                queue.add(new TreeNodeWithLevel(tl.treeNode.left, tl.level + 1));
            }
            if (tl.treeNode.right != null) {
                queue.add(new TreeNodeWithLevel(tl.treeNode.right, tl.level + 1));
            }
        }
        return result;
    }

    private List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> levelOrder2(TreeNode root) {
        /**
         * 递归实现：记录每个节点的层号
         */
        if (root == null) {
            return result;
        }
        dfs(root, 0);
        return result;
    }

    private void dfs(TreeNode root, int level) {
        if (root == null) {
            return;
        }
        if (level > result.size() - 1) {
            result.add(new ArrayList<>());
        }
        result.get(level).add(root.val);
        dfs(root.left, level + 1);
        dfs(root.right, level + 1);
    }

    public List<List<Integer>> levelOrder3(TreeNode root) {
        /**
         * 根据每层size分层
         * 队列实现，每层的节点添加完继续下一层
         */
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            // 每一层new list
            List<Integer> curList = new ArrayList<>();
            int levelSize = queue.size();
            for (int i = 0; i < levelSize; i++) {
                // 遍历当前层节点
                TreeNode node = queue.poll();
                curList.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            result.add(curList);
        }
        return result;
    }

    class TreeNodeWithLevel {
        TreeNode treeNode;
        int level;

        public TreeNodeWithLevel(TreeNode treeNode, int level) {
            this.treeNode = treeNode;
            this.level = level;
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}